<template>
	<!-- 搜索页面 -->
	<view class="search-box">
		<view style="background-color: #C00000;">
			<uni-search-bar :focus="true" radius="100" placeholder="请输入搜索内容" clearButton="auto" cancelButton="none" @confirm="search" @input="input"/>
		</view>

	<!-- 搜索建议列表 -->
	<view class="sugg-list" v-if="searchResults.length !== 0">
		<view class="sugg-item" v-for="(item, i) in searchResults" :key="i" @click="gotoDetail(item)">
			<view class="goods-name">{{item.goods_name}}</view>
			<uni-icons type="arrowright" size="16"></uni-icons>
		</view>
	</view>
	
	<!-- 搜索历史 -->
	<view class="history-box" v-else>
		<!-- 标题 -->
		<view class="history-title">
			<text >搜索历史</text>
			<uni-icons type="trash" size="24" @click="dialogToggle('info')"></uni-icons>
		</view>
		<!-- 列表 -->
		<view class="history-list">
			<uni-tag :text="item" v-for="(item,index) in histories" :key="index" custom-style="background-color: #f4f4f4; border-color: #d1d0d6; color: #000000;" @click="gotoGoodsList(item)"></uni-tag>
		</view>
	</view>
	


			
	<view>
		<!-- 提示窗示例 -->
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog :type="msgType" cancelText="关闭" confirmText="同意" title="提示!" content="是否清空历史记录？" @confirm="dialogConfirm"
				@close="dialogClose"></uni-popup-dialog>
		</uni-popup>
	</view>
	
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer: null,
				keyword: '',
				// 搜索的结果列表
				searchResults: [],
				historyList: [],
				
				type: 'center',
				msgType: 'success',
				value: ''
			};
		},
		
		onLoad() {
			this.historyList = JSON.parse(uni.getStorageSync('keyword') || '[]')
		},
		
		methods: {
			// input 输入事件处理函数
			input(e) {
				// console.log(e)
			clearTimeout(this.timer)
				
			this.timer = setTimeout(() => {
					// console.log(e)
					this.keyword = e
					this.getSearchList()
				}, 500)
			},
			
			async getSearchList() {
				// 判断搜索关键词是否为空
				if(this.keyword.length === 0) {
					this.searchResults = []
					return
				}
				
				const {data: res} = await uni.$http.get('/api/public/v1/goods/qsearch', {query: this.keyword})
				if(res.meta.status !== 200) return uni.$showMsg()
				this.searchResults = res.message
				
				this.saveSearchHistory()
			},
			
			gotoDetail(item) {
				console.log(item.goods_id)
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
				})
			},
			
			saveSearchHistory() {
				// this.historyList.push(this.keyword)
				const set = new Set(this.historyList)
				
				set.delete(this.keyword)
				
				set.add(this.keyword)
				
				this.historyList = Array.from(set)
				
				// 对搜索历史持久化存储
				uni.setStorageSync('keyword', JSON.stringify(this.historyList))
			},
			
			cleanHistory() {
				this.historyList = []
				uni.setStorageSync('keyword', '[]')
			},
			
			dialogClose() {
							console.log('点击关闭')
						},
						
			dialogToggle(type) {
							this.msgType = type
							this.$refs.alertDialog.open()
						},
			
			dialogConfirm() {
							this.cleanHistory()
							uni.$showMsg("清除成功！")
						},
			
			gotoGoodsList(item) {
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?query=' + item
				})
			}
			
		},
		computed: {
			histories() {
				return [...this.historyList].reverse()
			}
		}
	}
</script>

<style lang="scss">
	.search-box {
		top: 0;
		position: sticky;
		z-index: 999;
	}
	
	.sugg-list {
		padding: 0, 10rpx;
		.sugg-item {
			display: flex;
			align-items: center;
			justify-content: space-between;
			font-size: 28rpx;
			padding: 26rpx;
			border-bottom: 3rpx solid #efefef;
			.goods-name {
				// 单行显示
				white-space: nowrap;
				// 超出部分隐藏
				overflow: hidden;
				// 隐藏为...
				text-overflow: ellipsis;
			}
		}
	}
	
	.history-box {
		.history-title {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 13rpx;
			border-bottom: 8rpx solid #efefef;
			height: 80rpx;
		}
		
		.history-list {
			display: flex;
			flex-wrap: wrap;
			margin-top: 20rpx;
			padding:  0 20rpx;
			.uni-tag {
				margin-top: 10rpx;
				margin-right: 20rpx;
				font-weight:bold;
			}
		}
	}
</style>
